<?php
require_once 'session.php';
$type=$_REQUEST['type'];
$time=$_REQUEST['time'];
$result="";
$time1="";
$expend_head=["微信openID","名字","电话","订单号","是否优惠","消费金额","消费时间","有无奖品","是否兑换","礼品名字","兑换时间"];
$bonus_head=["微信openID","名字","电话","提现金额","完成时间"];
switch($time){
    case "lm" :$time1=date("Y")."年".((date("m"))-1)."月";break;
    case "m" :$time1=date("Y")."年".date("m")."月";break;
    case "ly" :$time1=((date("Y"))-1)."年";break;
    case "y" :$time1=date("Y")."年";break;
}
if($type=='expend'){
    $expend=$core->getAllExpendWhen($time);
    $total=$core->getAllExpendWhenTotal($time);
    $file_name=$time1."消费记录报表";
    $result= table($expend,$expend_head,$total);

}else if($type=='bonus'){
    $bonus=$core->GetFinishWithdrawWhen($time);
    $total=$core->GetFinishWithdrawWhenTotal($time);
    $file_name=$time1."佣金提现报表";
    $result= table($bonus,$bonus_head,$total);
}
$file_name.=".xls";
header("Content-type: application/octet-stream;charset=utf8");
//header("Accept-Ranges: bytes");
header("Accept-Length: ".count($result));
header("Content-Disposition: attachment; filename=".iconv('UTF-8', 'GBK//IGNORE', $file_name));

echo $result;

function table($arr,$head,$total){
    $strbuff="<table><tr>";
    foreach($head as $v){
        $strbuff.="<td>$v</td>";
    }
    $strbuff.="</tr>";
    for($a=0;$a<count($arr);$a++){
        $strbuff.="<tr>";
        foreach($arr[$a] as $k=>$v){
            if($k=="is_benefit"||$k=="allow_barter"||$k=="finish_barter"){
                if($v=="1"){
                    $v="是";
                }else{
                    $v="否";
                }
            }
            $strbuff.="<td> $v </td>";
        }
        $strbuff.="</tr>";
    }
    $strbuff.="<tr>";
    foreach($arr[0] as $k=>$v){
        if($k=="money"){
            $strbuff.="<td>".$total["total_money"]."</td>";
        }else{
            $strbuff.="<td></td>";
        }
    }
    $strbuff.="</tr>";
    $strbuff.="</table>";
    return $strbuff;
}